﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Titan;
using StockMatch.Service.Entity;

namespace StockMatch.Service
{
    /// <summary>
    /// 方案配置
    /// </summary>
    public class ProjectSettingService : ContextBoundObject
    {

        #region 增删改

        public bool Insert(ProjectSetting projectSetting)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Insert(projectSetting) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public bool Update(ProjectSetting projectSetting, PropertyExpression[] updateProperty)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Update(projectSetting, updateProperty) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex);
            }
        }

        public bool Delete(int projectSettingId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var projectSetting = new ProjectSetting() { ProjectSettingId = projectSettingId };
                    return cn.Delete(projectSetting) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion


        #region 查询


        public ProjectSetting GetProjectSetting(int projectSettingId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var projectSetting = new ProjectSetting() {ProjectSettingId = projectSettingId};
                    if (cn.Select(projectSetting))
                    {
                        return projectSetting;
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public ProjectSettings GetProjectSettingsByQuery(QueryExpression query)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    ProjectSettings projectSettings = new ProjectSettings();
                    projectSettings.TotalCount = cn.SelectCollection(projectSettings.Items, query);
                    return projectSettings;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }


        /* * 
         * 
        //根据方案ID查询方案的配置
        public ProjectSettings GetProjectSettingByProjectId(int projectId)
        {
            var projectSettings = new ProjectSettings();
            try
            {
                using (var cn=Environment.OpenSession())
                {
                    var query = new QueryExpression();
                    query.Selects.Add(ProjectSetting_.ALL);
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }
         * */

        #endregion
    }
}
